#ifndef __NETCOMM_H
#define __NETCOMM_H

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string>
#include <map>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <pthread.h>

#include "CommonShare.h"

#include "HttpCmdBase.h"

#define CLIENT_MAX_NUM 5

// COMMAND TYPE
#define CASUAL_STR 0x00

#define DEVICE_NAME 0xE
#define DEVICE_TIME 0xF

// 接收缓冲区
static char m_recvBuf[30];

// 发送缓冲区
static char m_sendBuf[100];
static char m_sendDataSize;

// 若置起此标志，发送方不需要发送数据
static bool m_NoAckFlag;

typedef struct MultiSocketStu
{
    pthread_t tid;
    int socketFd;
} MULTI_SOCKET_STU;

static MULTI_SOCKET_STU g_SocketStuArray[CLIENT_MAX_NUM];

class CNetComm
{

public:
    CNetComm();
    ~CNetComm(); 
    int TcpServerHttp();
    int SocketInit(int port);

private:
    bool AddNewClient(const int *newClientFd);
    void CloseClient(const int *newClientFd);
    void CloseAllClient();

    static void *ThreadFunc(void *arg);

    static void *TcpHandler_Reverse_RoutineFunc(void *arg); // reverse message , and send it back
    int TcpHandler_HttpServer_RoutineFunc(int);             // Http Server


    // static void *threadRecvFunc(void *arg);
    static void ServerBaseFunc();
    static void SendDataBySocket(int socketFd);

    // 详细的处理函数
    static void LoadDeviceParam(char commandType);

    // int m_clientFd[CLIENT_MAX_NUM];

    // HTTP
    bool HttpBasicAuth(const std::string &strAll);
    bool GetHttpReqCmd(const std::string &strAll, std::string &strCmd);
    bool GetHttpReqData(const std::string &strAll, std::string &strBody);

    bool AssemblyHttpResponse(const std::string &strBody, std::string &strAll);

private:
    std::map<std::string, CHttpCmdBase*> m_httpCmdObjMap;
};

#endif